var express = require('express');
var router = express.Router();
var request = require('../http_request.js');



router.get('/tianping', function(req, res, next) {  //甜品
	request('/gonggao/select/all','GET',{})         //获取公告内容
	.then(function (data) {
		res.render('indextianping',{data:JSON.parse(data)});
		});

});
/* GET home page. */
router.get('/', function(req, res, next) { 
	request('/gonggao/select/all','GET',{})         //获取公告内容 和首页商品内容
	.then(function (data) {
		res.render('index',{data:JSON.parse(data)});
	});
});
router.get('/dingdan', function(req, res, next) { 
	request('/dingdan/select/all','GET',{})         //获取订单内容
	.then(function (data) {
		res.render('shopp_dingdan',{data:JSON.parse(data)});
	});
});

router.get('/login', function(req, res, next) { 
		res.render('login',{});
});
router.post('/denglu/handler',function (req, res, next) {
	request('/denglu/handler','POST',req.body)
	.then(function (data) {
		data = JSON.parse(data); 	
		if(data.status==1){
			res.redirect('/');
		}else{
			res.redirect('/cuowu.html');
		}
	});
});

router.get('/login_close_handler',function(req,res,next){       /*退出登录*/
   request('/login_close_handler','POST',{name:req.param('name')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		// console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/');
		}else{
			res.send(data.msg);
		}
	});
});

                                                      /*聊天框开始*/
router.get('/liaotian', function(req, res, next) { 
	request('/liaotian/select/all','GET',{})         //获取订单内容
	.then(function (data) {
		res.render('liaotian',{data:JSON.parse(data)});
	});
});

router.post('/liaotian/handler',function (req, res, next) {
	request('/liaotian/handler','POST',req.body)
	.then(function (data) {
		data = JSON.parse(data); 	
		if(data.status==1){
			res.redirect('/liaotian');
		}else{
			res.send(data.msg);
		}
	});
});

router.post('/admin/liaotian/handler',function (req, res, next) {
	request('/admin/liaotian/handler','POST',req.body)
	.then(function (data) {
		data = JSON.parse(data); 	
		if(data.status==1){
			res.redirect('/admin/message');
		}else{
			res.send(data.msg);
		}
	});
});


router.get('/chexiao/handler',function (req, res, next) {
	request('/delete/message/handler','POST',{id:req.param('id')})
	.then(function (data) {
		data = JSON.parse(data); 	
		if(data.status==1){
			res.redirect('/liaotian');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/admin/message',function (req, res, next) {
	request('/adminmessage/select/all','GET',{})                       
	.then(function (data) {
		res.render('guanlimessage',{data:JSON.parse(data)});
	});
});

router.get('/admindelete/message/handler',function (req, res, next) {
	request('/admindelete/message/handler','POST',{})
	.then(function (data) {
		data = JSON.parse(data); 	
		if(data.status==1){
			res.redirect('/admin/message');
		}else{
			res.send(data.msg);
		}
	});
});
                                                                      /*聊天框结束*/


                                                                    /*购物车开始*/
router.get('/shopp_addcar_handler',function(req,res,next){       /*添加商品至购物车*/
   request('/shopp_addcar_handler','POST',{b_shoppTu:req.param('shoppTu'),b_price:req.param('price'),b_shopp:req.param('shopp')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		if(data.status==1){
			res.redirect('/shopp_car');
		}else{
			res.send(data.msg);
		}
	});
});



router.get('/shopp_car', function(req, res, next) {                         
	request('/user/shopp_car','GET',{})         //购物车内容
	.then(function (data) {
		res.render('shopp_car',{data:JSON.parse(data)});
	});
});


router.get('/shopp_delete_handler',function(req,res,next){       /*删除购物车的某一行数据*/
   request('/shopp_delete_handler','POST',{id:req.param('id')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		// console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/shopp_car');
		}else{
			res.send(data.msg);
		}
	});
});


router.get('/shopp_xiugai_handler',function(req,res,next){       /*修改购物车商品数量*/
   request('/shopp_xiugai_handler','POST',{id:req.param('id'),b_number:req.param('num')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/shopp_car');
		}else{
			res.send(data.msg);
		}
	});
});



router.get('/shopp_jiezhang_handler',function(req,res,next){       /*结账付款*/
   request('/shopp_jiezhang_handler','POST',{id:req.param('id')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		// console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/shopp_car');
		}else{
			res.send(data.msg);
		}
	});
});
                                                             /*购物车结束*/



router.get('/guanli', function(req, res, next) {          /*郭西开始*/
  // res.render('index', { title: 'Express' });
  request('/guanli','GET',{})
	.then(function (data) {
		res.render('guanli',{data:JSON.parse(data)});
	});
});

router.get('/add_shopp',function (req, res, next) {
	res.render('add_shopp',{});
});

router.post('/add_shopp/handler',function (req, res, next) {
	request('/add_shopp/handler','POST',req.body)
	.then(function (data) {
		data = JSON.parse(data);
		if(data.status==1){
			res.redirect('/guanli');
		}else{
			res.send(data.msg);

		}
	});
});


router.get('/shopp_updata', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  request('/shopp_updata','GET',{})
	.then(function (data) {
		res.render('shopp_updata',{data:JSON.parse(data)});
	});
});



router.get('/shopp_update_handler',function(req,res,next){       /*修改商品价格*/
   request('/shopp_update_handler','POST',{id:req.param('id'),e_price:req.param('e_price')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('shopp_updata');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/shopp_update_name',function(req,res,next){       /*修改商品名字*/
   request('/shopp_update_name','POST',{id:req.param('id'),e_shopp:req.param('e_shopp')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('shopp_updata');
		}else{
			res.send(data.msg);
		}
	});
});


router.get('/shopp_update_shoppLeixing',function(req,res,next){       /*修改商品类型*/
   request('/shopp_update_shoppLeixing','POST',{id:req.param('id'),e_shoppLeixing:req.param('e_shoppLeixing')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('shopp_updata');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/shopp_update_zongnumber',function(req,res,next){       /*修改商品库存*/
   request('/shopp_update_zongnumber','POST',{id:req.param('id'),e_zongnumber:req.param('e_zongnumber')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('shopp_updata');
		}else{
			res.send(data.msg);
		}
	});
});


router.get('/delete_shopp', function(req, res, next) {       
  // res.render('index', { title: 'Express' });
  request('/delete_shopp','GET',{})
	.then(function (data) {
		res.render('delete_shopp',{data:JSON.parse(data)});
	});
});

router.get('/delete_handler',function(req,res,next){       /*删除数据*/
   request('/delete_handler','POST',{id:req.param('id')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/delete_shopp');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/guanlidingdan', function(req, res, next) {       
  // res.render('index', { title: 'Express' });
  request('/guanlidingdan','GET',{})
	.then(function (data) {
		res.render('guanlidingdan',{data:JSON.parse(data)});
	});
});
router.get('/guanlidingdan_handler',function(req,res,next){       /*订单管理*/
   request('/guanlidingdan_handler','POST',{bianhao:req.param('bianhao')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/guanlidingdan');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/guanlidingdanFaHuo_handler',function(req,res,next){       /*订单发货*/
   request('/guanlidingdanFaHuo_handler','POST',{bianhao:req.param('d_bianhao')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		// console.log(req.param('d_bianhao'));
		if(data.status==1){
			res.redirect('/guanlidingdan');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/gonggao', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  request('/gonggao','GET',{})
	.then(function (data) {
		res.render('gonggao',{data:JSON.parse(data)});
	});
});



router.get('/gonggoa_update_handler',function(req,res,next){       /*修改商品价格*/
   request('/gonggoa_update_handler','POST',{id:req.param('id'),f_name:req.param('f_name')})
	.then(function (data) {
		data = JSON.parse(data); 
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('gonggao');
		}else{
			res.send(data.msg);
		}
	});
});

router.get('/guanlihuiyuan', function(req, res, next) { /*会员管理*/      
  // res.render('index', { title: 'Express' });
  request('/guanlihuiyuan','GET',{})
  .then(function (data) {
  	res.render('guanlihuiyuan',{data:JSON.parse(data)});
  });
});


router.get('/huiyuan_handler',function(req,res,next){       /*删除数据*/
	request('/huiyuan_handler','POST',{id:req.param('id')})
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		// console.log(data.status);
		console.log(req.param('id'));
		if(data.status==1){
			res.redirect('/guanlihuiyuan');
		}else{
			res.send(data.msg);
		}
	});
});


router.get('/yidaoer', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  request('/yidaoer','GET',{})
  .then(function (data) {
  	res.render('yidaoer',{data:JSON.parse(data)});
  });
});

router.get('/wubaidaoyiqian', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  request('/wubaidaoyiqian','GET',{})
  .then(function (data) {
  	res.render('wubaidaoyiqian',{data:JSON.parse(data)});
  });
});


router.get('/YQYS', function(req, res, next) {
  // res.render('index', { title: 'Express' });
  request('/YQYS','GET',{})
  .then(function (data) {
  	res.render('YQYS',{data:JSON.parse(data)});
  });
});

router.get('/xianqing', function(req, res, next) {/*详情*/
  // res.render('index', { title: 'Express' });
  request('/xianqing','POST',{id:req.param('id')})
  .then(function (data) {
  	res.render('xianqing',{data:JSON.parse(data)});
  });
});

router.get('/pingjia/handler', function(req, res, next) {/*商品详情的 商品评价*/
  request('/pingjia/handler','POST',{id:req.param('id'),message:req.param('message')})
  .then(function (data) {
  	res.redirect('/pingJiaChengGong.html');
  });
});
/*郭西结束*/


/*管理登录*/
router.get('/hregist',function (req, res, next) {
	res.render('hregist',{});
});
router.post('/gdenglu/handler',function (req, res, next) {
	request('/gdenglu/handler','POST',req.body)
	.then(function (data) {
		console.log(data);
		data = JSON.parse(data); //有问题啊
		
		if(data.status==1){
			res.redirect('/guanli');
		}else{
			res.redirect('/GuanliLogin.html');
		}
	});
});


/*龚中华注册开始*/
//个人信息
router.get('/gerenxinxi',function (req, res, next) {
	request('/username/select','GET',{})
	.then(function (data) {
		res.render('gerenxinxi',{data:JSON.parse(data)});
	});
});

//修改信息
router.get('/huiyuan',function (req, res, next) {
	request('/username/select','GET',{})
	.then(function (data) {
		res.render('huiyuan',{data:JSON.parse(data)});
	});
});


//修改提交
router.post('/huiyuan/xiugai/handler',function (req, res, next) {
	request('/huiyuan/xiugai','POST',req.body)
	.then(function (data) {
		console.log("dsasasa");
		data = JSON.parse(data); //有问题啊
		console.log(data.status);
		if(data.status==1){
			res.redirect('/');
		}else{
			res.send(data.msg);
		}
	});
});

//注册界面
router.get('/zhuce',function (req, res, next) {
	res.render('zhuce',{});//指文件
});

//注册提交
router.post('/zhuce/handler',function (req, res, next) {
	request('/zhuce/handler','POST',req.body)
	.then(function (data) {
		data = JSON.parse(data); //有问题啊
		console.log(data.status);
		if(data.status==1){
			res.redirect('/login');
		}else{
			res.redirect('/cuowu1.html');
		}
	});
});



module.exports = router;
